<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="订单号">
                <a-input placeholder="请输入订单号" v-model="queryParam.order_no"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="起止时间">
                <a-range-picker
                  format="YYYY-MM-DD HH:mm"
                  valueFormat="YYYY-MM-DD HH:mm"
                  :placeholder="['开始时间', '结束时间']"
                  v-model="queryTime"
                  @change="changeTime"
                />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getList">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>

    <a-card :bordered="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="0"> 待确认 </a-radio-button>
          <a-radio-button value="1"> 已发货 </a-radio-button>
          <a-radio-button value="2"> 已完成 </a-radio-button>
        </a-radio-group>
      </div>

      <ListTable
        ref="listTable"
        :list_th="list_th"
        @applyAfterSale="applyAfterSale"
        @send="send"
        :pagaData="list"
        @receipt="receipt"
      >
      </ListTable>

      <div style="text-align: right">
        <a-pagination v-model="queryParam.page" :total="totalCount" @change="tablePageChange" />
      </div>

      <!-- @triggerAction="listActions" -->

      <!-- <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange
        }"
        :columns="columns"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <div slot="name" slot-scope="text, record">
          <div class="name">
            <div class="info">
              <span>{{ record.supplier.name }}</span>
              <span>{{ record.supplier.tel }}</span>
            </div>
          </div>
        </div>
        <div slot="goods" slot-scope="scope">
          <div class="name" v-for="(item, index) in scope.goods_list" :key="index" style="margin-bottom:10px;">
            <div class="pic">
              <img :src="item.pic_url" alt="" />
            </div>
            <div class="info">
              <span>{{ item.name }}</span>
              <span>{{ item.attr_name }}</span>
            </div>
          </div>
        </div>
        <span slot="mobile" slot-scope="scope"> {{ scope.name }} / {{ scope.mobile }} </span>
        <span slot="audit_time" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <span slot="is_show_refund" slot-scope="text">
          {{ getStatus(text) }}
        </span>

        <span slot="status" slot-scope="text">
          <span class="tag" :style="text | orderStatus"></span>
          <span>
            {{ getStatus2(text) }}
          </span>
        </span>

        <span slot="refuse_desc" slot-scope="text, goods">
          <span>{{ text }}</span>
        </span>

        <span class="table-action" slot="action" slot-scope="text, goods">
          <a v-if="goods.is_show_refund === 1" @click="applyAfterSale(goods)">申请售后</a>
          <a v-else-if="goods.is_show_refund === 3" @click="send(goods)">发货</a>
          <span v-else>-</span>
        </span>
      </a-table> -->
    </a-card>

    <a-modal title="申请售后" :visible="showPop" @cancel="showPop = false" :width="800">
      <template slot="footer">
        <a-button key="submit" type="primary" :loading="btnLoading" @click="submitAdd"> 确定 </a-button>
      </template>

      <a-form-model
        ref="ruleForm"
        :rules="formRules"
        :model="form"
        :label-col="{ span: 3 }"
        :wrapper-col="{ span: 12 }"
      >
        <a-form-model-item label="售后类型" prop="type">
          <a-radio-group v-model="form.type">
            <a-radio :value="1">退货退款</a-radio>
            <a-radio :value="2">换货</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-item prop="type" label="选择售后原因" v-if="reasonsType && reasonsType.length > 0">
          <a-select placeholder="选择售后原因" v-model="form.reasonsTypeCode">
            <a-select-option v-for="item of reasonsType" :value="item.code" :key="item.code">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item prop="type" label="选择返件类型" v-if="returnType && returnType.length > 0">
          <a-select placeholder="选择返件类型" v-model="form.returnTypeCode">
            <a-select-option v-for="item of returnType" :value="item.code" :key="item.code">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item prop="type" label="选择包装情况" v-if="packageType && packageType.length > 0">
          <a-select placeholder="选择包装情况" v-model="form.packageTypeCode">
            <a-select-option v-for="item of packageType" :value="item.code" :key="item.code">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-model-item label="退款说明" prop="desc">
          <a-input v-model="form.desc" placeholder="请输入退款说明" />
        </a-form-model-item>

        <a-form-model-item v-if="form.type !== 2" label="退款金额" prop="refund_price">
          <a-input-number
            style="width: 376px"
            placeholder="请输入"
            v-model="form.refund_price"
            :precision="2"
            suffix="元"
            :min="0"
          />
        </a-form-model-item>

        <a-form-model-item label="凭证图片">
          <cyyUploadImg
            @uploadSuccess="
              (e) => {
                form.pic_list = e.data
              }
            "
            @deleteImg="
              (e) => {
                form.pic_list.splice(e.index, 1)
              }
            "
            dataType="list"
            :imgList.sync="form.pic_list"
          >
          </cyyUploadImg>
        </a-form-model-item>

        <a-form-model-item label="凭证视频">
          <cyyUploadVideo
            :videoSrc="form.video"
            @deleteVideo="form.video = ''"
            @uploadSuccess="
              (e) => {
                form.video = e.data
              }
            "
          ></cyyUploadVideo>
        </a-form-model-item>
      </a-form-model>
    </a-modal>

    <SendModal
      ref="SendModal"
      :visible.sync="isShowSendModal"
      :expressList="expressList"
      @getData="tablePageChange(1)"
    ></SendModal>
  </page-header-wrapper>
</template>

<script>
import SendModal from './components/SendModal'
import { transRecord, mchOrderRefundApply, setDistributionStatus } from '@/api/purchase/business'
import tabList from '@/views/saas/components/tabList.vue'
import ListTable from './components/ListTable.vue'
import { cyyUploadVideo, cyyUploadImg } from '@/components/CyyUi/index'
// const columns = [
//   {
//     title: 'ID',
//     dataIndex: 'id'
//   },
//   {
//     title: '订单号',
//     dataIndex: 'order_no'
//   },
//   {
//     title: '商品',
//     scopedSlots: {
//       customRender: 'goods'
//     },
//     width: 200
//   },
//   {
//     title: '商品金额',
//     dataIndex: 'pay_price',
//     scopedSlots: {
//       customRender: 'pay_price'
//     },
//     width: 150
//   },
//   {
//     title: '买家电话',
//     scopedSlots: {
//       customRender: 'mobile'
//     }
//   },
//   {
//     title: '订单状态',
//     dataIndex: 'status',
//     scopedSlots: {
//       customRender: 'status'
//     }
//   },
//   {
//     title: '售后状态',
//     dataIndex: 'is_show_refund',
//     scopedSlots: {
//       customRender: 'is_show_refund'
//     }
//   },
//   {
//     title: '供货商',
//     scopedSlots: {
//       customRender: 'name'
//     },
//     width: 200
//   },
//   {
//     title: '拒绝原因',
//     dataIndex: 'refuse_desc',
//     scopedSlots: {
//       customRender: 'refuse_desc'
//     }
//   },
//   {
//     title: '操作',
//     dataIndex: 'action',
//     scopedSlots: { customRender: 'action' }
//   }
// ]

export default {
  components: {
    ListTable,
    tabList,
    cyyUploadImg,
    cyyUploadVideo,
    SendModal,
  },

  // filters: {
  //   orderStatus(val) {
  //     if (val == 0) {
  //       return 'background-color:#faad14;'
  //     }

  //     if (val == 1 || val == 2) {
  //       return 'background-color:#65c817;'
  //     }
  //   }
  // },

  // computed: {
  //   getStatus2() {
  //     return status => {
  //       let str

  //       switch (status) {
  //         case '0':
  //           str = '待确认'
  //           break

  //         case '1':
  //           str = '已发货'
  //           break

  //         case '2':
  //           str = '已完成'
  //           break
  //       }

  //       return str
  //     }
  //   },

  //   getStatus() {
  //     return is_show_refund => {
  //       let str

  //       switch (is_show_refund) {
  //         case -1:
  //           str = '等待处理'
  //           break

  //         case 1:
  //           str = ''
  //           break

  //         case 2:
  //           str = '已拒绝'
  //           break

  //         case 3:
  //           str = '待商家发货'
  //           break

  //         case 4:
  //           str = '等待供货商确认收货'
  //           break

  //         case 5:
  //           str = '已经打款'
  //           break

  //         case 6:
  //           str = '已经退换货'
  //           break
  //       }

  //       return str
  //     }
  //   }
  // },

  data() {
    return {
      form: {
        type: 1,
        desc: '',
        pic_list: [],
        video: '',
        refund_price: '',
        reasonsTypeCode: 0,
        returnTypeCode: 0,
        packageTypeCode: 0,
      },

      formRules: {
        type: [{ required: true, message: '请选择售后类型', trigger: 'change' }],
        desc: [{ required: true, message: '请填写退款说明', trigger: 'blur' }],
        refund_price: [{ required: true, message: '请输入退款金额', trigger: 'blur' }],
      },

      order_id: '',

      tabList: [
        {
          key: 'purchaseGoodsList',
          name: '云仓选品库',
        },
        {
          key: 'goodsCloudList',
          name: '我的云仓',
        },
        {
          key: 'purchaseTransRecord',
          name: '云仓转单',
        },
        {
          key: 'businessPurchaseBalance',
          name: '我的账户',
        },
        {
          key: 'purchaseSetting',
          name: '云仓设置',
        },
        {
          key: 'purchaseGoodsUpdateLog',
          name: '商品更新记录',
        },
      ],
      // 搜索参数
      queryParam: {
        page: 1,
        limit: 10,
        status: '-1',
        start_time: '',
        end_time: '',
        order_no: '',
      },
      // columns,
      list: [],
      // 是否显示创建供货商弹窗
      showPop: false,

      queryTime: [],
      btnLoading: false,

      // 发货
      expressList: [],
      isShowSendModal: false,

      list_th: [
        { type: 'list', title: '商品信息', span: 4 },
        { type: 'list', title: '单价/数量', span: 3 },
        { type: 'list', title: '运费/优惠', span: 2 },
        { type: 'list', title: '订单金额', span: 2 },
        { type: 'list', title: '买家/配送方式', span: 3 },
        { type: 'list', title: '供货商/下单时间', span: 3 },
        { type: 'list', title: '订单状态', span: 3 },
        { type: 'list', title: '配送方式', span: 3 },
        { type: 'list', title: '售后状态', span: 2 },
        { type: 'list', title: '拒绝原因', span: 2 },

        {
          type: 'list',
          title: '操作',
          span: 3,
          actions: ['编辑', '发货', '取消', '核销', '确认收货', '订单打印', '小票打印', '添加备注', '移入回收站'],
        },
      ],
      reasonsType: null,
      returnType: null,
      packageType: null,
    }
  },

  watch: {
    showPop(e) {
      if (!e) {
        this.form.pic_list = ''
        this.form.video = ''
        this.$refs.ruleForm.resetFields()
      }
    },
  },

  created() {
    this.getList()
  },

  methods: {
    receipt({ id: order_id }) {
      this.$confirm({
        title: '提示',
        content: '是否确认收货？',
        onOk: async () => {
          const hide = this.$message.loading('确认中...', 0)
          const res = await setDistributionStatus({
            order_id,
          })

          hide()
          if (!res.code) {
            this.$message.success(res.msg)
            this.getList()
            return
          }

          this.$message.error(res.msg)
        },
        onCancel: () => {},
      })
    },
    submitAdd() {
      this.$refs['ruleForm'].validate(async (valid) => {
        if (valid) {
          let apply_data = JSON.parse(JSON.stringify(this.form))
          this.btnLoading = true

          if (apply_data.refund_price) {
            apply_data.refund_price = parseFloat(apply_data.refund_price).toFixed(2)
          } else {
            delete apply_data.refund_price
          }

          const res = await mchOrderRefundApply({
            apply_data,
            order_id: this.order_id,
          })

          this.btnLoading = false
          if (res.code == 0) {
            this.$message.success('操作成功')
            this.getList()
            this.showPop = false
          } else {
            this.$message.error(res.msg)
          }
        } else {
          return false
        }
      })
    },

    // 分页请求
    tablePageChange(e) {
      this.queryParam.page = e
      this.getList()
    },

    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中...', 1000)
      const res = await transRecord(this.queryParam)
      if (res.code == 0) {
        this.list = res.data.list
        this.totalCount = res.data.count
      }
      hide()
    },

    // 切换筛选时间
    changeTime(e) {
      if (e.length) {
        this.queryParam.start_time = e[0]
        this.queryParam.end_time = e[1]
      } else {
        this.queryParam.start_time = ''
        this.queryParam.end_time = ''
      }
    },

    // 申请售后
    applyAfterSale({ id, pay_price, reasonsType, returnType, packageType }) {
      this.order_id = id
      this.reasonsType = reasonsType
      this.returnType = returnType
      this.packageType = packageType
      this.showPop = true
      this.form.refund_price = pay_price
    },

    // 发货
    send(item) {
      let { express: expressList, mobile, name, detail, id: order_id, zipcode } = item

      this.expressList = expressList
      this.$refs.SendModal.form = {
        order_id,
        user_send_express: '',
        user_send_express_no: '',
        mobile,
        name,
        detail,
        zipcode,
      }
      this.isShowSendModal = true
    },
  },
}
</script>
<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 15px;
  }
}
.tip {
  line-height: normal;
  margin-bottom: 0;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}

.tag {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-right: 4px;
}
</style>
